/**
 * Created by YHQ on 2016-03-26.
 */
angular.module('hyt.zddzService',[])
    .factory('ZddzListFactory',function($rootScope,ENV,$resource,Storage,$ionicLoading,$state){
        var pagesize=10;
        var caiwuListData={};
        var APIUrl= Storage.get('fwqdz');
        var resource=$resource(APIUrl);
        return{
            queryList:function(sid,user_id,yh_id,dw_id,ddz,kssj,jssj,cyzt,bm_id){
                var hasNextPage =true;    //判断是否有下一页
                return resource.save({
                    'ACTION_NAME':'sqlKeyBiz.page#zhandian_duizhang_query_new',
                    'SID':sid,
                    'USER_ID':user_id,
                    'ACTION_INFO':{
                        'pagesize':pagesize,
                        'current_page':'1',
                        'djsj_ks':kssj,
                        'djsj_js':jssj,
                        'shrdh':'',
                        'fhrdh':'',
                        'fhrmc':'',
                        'shbm_id':ddz,
                        'dw_id':dw_id,
                        'yh_id':yh_id,
                        'cyzt':cyzt,
                        'cybm_id':bm_id
                    }
                },function(resp){
                    var resp=resp;
                    if(resp.ACTION_RETURN_CODE == "000000"){
                        if(resp.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        caiwuListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : resp.ACTION_INFO.RESULT_LIST
                        };
                        $rootScope.$broadcast('ZddzList.zddzUpdated');
                    }else if (resp.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                    //console.log(caiwuListData[pagesize].data);
                })
            },
            //获取站点间财务列表单页数据
            getCaiwu : function(){
                if(caiwuListData[pagesize] == undefined){
                    return false;
                }
                return caiwuListData[pagesize];
            },

            //获取站点间财务列表更多页数据
            getMoreCaiwu:function(){

                //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
                if(caiwuListData[pagesize] == undefined){
                    return false;
                }
                //获取以前的数据
                var hasNextPage = caiwuListData[pagesize].hasNextPage;
                var current_page = caiwuListData[pagesize].current_page;
                var moreCydListData = caiwuListData[pagesize].data;
                //console.log(moreCydListData);
            },
            //有下一页
            hasNextPage: function() {
                if (caiwuListData[pagesize] === undefined) {
                    return false;
                }
                return caiwuListData[pagesize].hasNextPage;
            },
            queryListBack:function(sid,user_id,yh_id,dw_id,cybm_id,kssj,jssj,cyzt,shbm_id){
                var hasNextPage =true;    //判断是否有下一页
                return resource.save({
                    'ACTION_NAME':'sqlKeyBiz.page#zhandian_duizhang_query_new',
                    'SID':sid,
                    'USER_ID':user_id,
                    'ACTION_INFO':{
                        'pagesize':pagesize,
                        'current_page':'1',
                        'djsj_ks':kssj,
                        'djsj_js':jssj,
                        'shrdh':'',
                        'fhrdh':'',
                        'fhrmc':'',
                        'shbm_id':shbm_id,
                        'dw_id':dw_id,
                        'yh_id':yh_id,
                        'cyzt':cyzt,
                        'cybm_id':cybm_id
                    }
                },function(resp){
                    var resp=resp;
                    if(resp.ACTION_RETURN_CODE == "000000"){
                        if(resp.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        caiwuListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : resp.ACTION_INFO.RESULT_LIST
                        };
                        $rootScope.$broadcast('ZddzListBack.zddzUpdated');
                    }else if (resp.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                    //console.log(caiwuListData[pagesize].data);
                })
            },
            //获取站点间财务列表单页数据
            getCaiwuBack : function(){
                if(caiwuListData[pagesize] == undefined){
                    return false;
                }
                return caiwuListData[pagesize];
            },

            //获取站点间财务列表更多页数据
            getMoreCaiwuBack:function(){

                //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
                if(caiwuListData[pagesize] == undefined){
                    return false;
                }
                //获取以前的数据
                var hasNextPage = caiwuListData[pagesize].hasNextPage;
                var current_page = caiwuListData[pagesize].current_page;
                var moreCydListData = caiwuListData[pagesize].data;
                //console.log(moreCydListData);
            },
            //有下一页
            hasNextPage: function() {
                if (caiwuListData[pagesize] === undefined) {
                    return false;
                }
                return caiwuListData[pagesize].hasNextPage;
            }
        }
    })

    .factory('ZdFactory',function($rootScope,ENV,$resource,Storage){
        var APIUrl= Storage.get('fwqdz'),zdList;
        var resource=$resource(APIUrl);
        return{
            queryZd:function(dw_id,sid,user_id,yh_id,zd_id){
                return resource.save({
                    'ACTION_NAME':'sqlKeyBiz.list#bmxx_query_by_zd_id',//sqlKeyBiz.list#zhandian_list
                    'SID':sid,
                    'USER_ID':user_id,
                    'ACTION_INFO':{
                        'dw_id':dw_id,
                        'yh_id':yh_id,
                        'zd_id':zd_id
                    }
                },function(resp){
                    zdList=resp;
                    console.log(zdList);
                    $rootScope.$broadcast('List.zdList');
                })
            },
            getZd:function(){
                return zdList;
            }
        }
    });

